
package com.jf.cloud.multishop.service.impl;

import com.jf.cloud.api.multishop.vo.ShopDetailVO;
import com.jf.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.jf.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.jf.cloud.api.user.feign.UserFeignClient;
import com.jf.cloud.api.user.vo.UserApiVO;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.multishop.dto.NoticeDTO;
import com.jf.cloud.multishop.mapper.NoticeMapper;
import com.jf.cloud.multishop.model.Notice;
import com.jf.cloud.multishop.model.ShopDetail;
import com.jf.cloud.multishop.service.NoticeService;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.vo.NoticeVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公告
 *
 * @author zz
 * @date 2020-11-25 17:57:56
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ShopDetailService shopDetailService;

    @Override
    public PageVO<NoticeVO> page(PageDTO pageDTO, NoticeDTO noticeDTO) {
        return PageUtil.doPage(pageDTO, () -> noticeMapper.list(noticeDTO));
    }

    @Override
    public NoticeVO getById(Long id) {
        NoticeVO noticeVO = noticeMapper.getById(id);
        if(StringUtils.isNotBlank(noticeVO.getMultiShopIds())){
            List<Long> multiShopIds = Arrays.stream(noticeVO.getMultiShopIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            List<ShopDetail> shopDetails = shopDetailService.listByShopIds(multiShopIds);
            List<ShopDetailVO> shopDetailList  = shopDetails.stream().map(shopDetail -> {
                ShopDetailVO shopDetailVO = new ShopDetailVO();
                shopDetailVO.setShopId(shopDetail.getShopId());
                shopDetailVO.setShopName(shopDetail.getShopName());
                shopDetailVO.setShopStatus(shopDetail.getShopStatus());
                shopDetailVO.setContactName(shopDetail.getContactName());
                shopDetailVO.setContactPhone(shopDetail.getContactPhone());
                shopDetailVO.setEmail(shopDetail.getEmail());
                return shopDetailVO;
            }).collect(Collectors.toList());
            noticeVO.setShopDetailVOList(shopDetailList);
        }
        if(StringUtils.isNotBlank(noticeVO.getUserIds())){
            List<Long> userIds = Arrays.stream(noticeVO.getUserIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            //可见范围的用户信息
            ServerResponseEntity<List<UserApiVO>> userResponese = userFeignClient.getUserByUserIds(userIds);
            if(userResponese.isSuccess()){
                noticeVO.setUserDtailList(userResponese.getData());
            }
        }
        //可见范围的供应商信息
        if(StringUtils.isNotBlank(noticeVO.getSupplierIds())){
            List<Long> supplierIds = Arrays.stream(noticeVO.getSupplierIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            ServerResponseEntity<List<SupplierApiDetailVO>> supplierResponse = supplierDetailFeignClient.listBySupplierIds(supplierIds);
            if(supplierResponse.isSuccess()){
                noticeVO.setSupplierDetailList(supplierResponse.getData());
            }
        }

        return noticeVO;
    }

    @Override
    public void updateIsLearning(Long id) {
       noticeMapper.updateIsLearning(id);
    }

    @Override
    public void save(Notice notice) {
        notice.setId(null);
        if (Objects.equals(notice.getStatus(), StatusEnum.ENABLE.value())) {
            notice.setPublishTime(new Date());
        }
        noticeMapper.save(notice);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.update(notice);
    }

    @Override
    public void deleteById(Long id) {
        noticeMapper.deleteById(id);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.NOTICES_KEY, key = "#shopId + ':' + #type")
    public void removeCacheByShopIdAndType(Long shopId, Integer type) {

    }

    @Override
    @Cacheable(cacheNames = CacheNames.NOTICES_KEY, key = "#shopId + ':' + #type")
    public List<NoticeVO> getlistByShopId(Long shopId, Integer type) {
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setShopId(shopId);
        noticeDTO.setType(type);
        noticeDTO.setStatus(StatusEnum.ENABLE.value());
        return noticeMapper.listAllByTypeAndShopId(noticeDTO);
    }

    @Override
    public PageVO<NoticeVO> pagePlatformNotice(PageDTO pageDTO, NoticeDTO noticeDTO) {
        return PageUtil.doPage(pageDTO, () -> noticeMapper.list(noticeDTO));
    }

    @Override
    public void saveBatch(List<Notice> noticeList) {
        for (Notice notice : noticeList) {
            notice.setId(null);
            if (Objects.equals(notice.getStatus(), StatusEnum.ENABLE.value())) {
                notice.setPublishTime(new Date());
            }
        }
        noticeMapper.saveBatch(noticeList);
    }
}
